package com.gzy.literature.dao;

import com.gzy.literature.entity.Users;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UsersDao {

    /**
     * 根据账号和密码登录用户
     *
     * @param account 用户账号
     * @param password 用户密码
     * @return 如果账号和密码匹配，则返回对应的用户对象；否则返回null
     */
    @Select("SELECT * FROM users WHERE account = #{account} AND password = #{password}")
    @Results(id = "userResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "account", column = "account"),
            @Result(property = "password", column = "password"),
            @Result(property = "email", column = "email"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "avatar", column = "avatar")
    })
    Users login(@Param("account") String account, @Param("password") String password);

    /**
     * 根据用户ID查询用户信息
     *
     * @param id 用户ID
     * @return 返回对应的用户对象
     */
    @Select("SELECT * FROM users WHERE id = #{id}")
    @Results(id = "userResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "account", column = "account"),
            @Result(property = "password", column = "password"),
            @Result(property = "email", column = "email"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "avatar", column = "avatar")
    })
    Users queryById(Integer id);

    /**
     * 根据条件查询用户列表（支持分页，但分页逻辑在Service层处理）
     *
     * @param users 包含查询条件的用户对象（如name, gender等）
     * @return 返回匹配条件的用户列表
     */
    @Select("<script>" +
            "SELECT * FROM users WHERE 1=1 " +
            "<if test='users.name != null'> AND name LIKE CONCAT('%', #{users.name}, '%')</if>" +
            "<if test='users.gender != null'> AND gender = #{users.gender}</if>" +
            "</script>")
    List<Users> queryAll(@Param("users") Users users);

    /**
     * 插入新用户信息
     *
     * @param users 包含用户信息的对象
     * @return 插入操作影响的行数（通常为1，表示成功插入一条记录）
     */
    @Insert("INSERT INTO users(name, gender, account, password, email, phone, avatar) VALUES(#{name}, #{gender}, #{account}, #{password}, #{email}, #{phone}, #{avatar})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Users users);

    /**
     * 更新用户信息
     *
     * @param users 包含更新后用户信息的对象
     * @return 更新操作影响的行数（如果匹配到记录，则通常为1）
     */
    @Update("<script>" +
            "UPDATE users " +
            "<set>" +
            "<if test='name != null'>name = #{name},</if>" +
            "<if test='gender != null'>gender = #{gender},</if>" +
            "<if test='account != null'>account = #{account},</if>" +
            "<if test='password != null'>password = #{password},</if>" +
            "<if test='email != null'>email = #{email},</if>" +
            "<if test='phone != null'>phone = #{phone},</if>" +
            "<if test='avatar != null'>avatar = #{avatar},</if>" +
            "</set>" +
            "WHERE id = #{id}" +
            "</script>")
    int update(Users users);

    /**
     * 根据用户ID删除用户信息
     *
     * @param id 用户ID
     * @return 删除操作影响的行数（如果匹配到记录，则通常为1）
     */
    @Delete("DELETE FROM users WHERE id = #{id}")
    int deleteById(Integer id);

    /**
     * 根据用户名查询用户
     *
     * @param username 用户名
     * @return 返回对应的用户对象
     */
    @Select("SELECT * FROM users WHERE name = #{username}")
    @Results(id = "userResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "account", column = "account"),
            @Result(property = "password", column = "password"),
            @Result(property = "email", column = "email"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "avatar", column = "avatar")
    })
    Users queryByUsername(String username);

    /**
     * 检查用户名是否已存在
     *
     * @param username 用户名
     * @return 返回匹配的用户数量，如果大于0则表示用户名已存在
     */
    @Select("SELECT COUNT(*) FROM users WHERE name = #{username}")
    int countByUsername(String username);

    /**
     * 通过账号查询用户信息
     *
     * @param account 用户账号
     * @return 返回对应的用户对象
     */
    @Select("SELECT * FROM users WHERE account = #{account}")
    @Results(id = "userResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "account", column = "account"),
            @Result(property = "password", column = "password"),
            @Result(property = "email", column = "email"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "avatar", column = "avatar")
    })
    Users queryByAccount(String account);

    /**
     * 通过邮箱查询用户信息
     *
     * @param email 用户邮箱
     * @return 返回对应的用户对象
     */
    @Select("SELECT * FROM users WHERE email = #{email}")
    @Results(id = "userResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "account", column = "account"),
            @Result(property = "password", column = "password"),
            @Result(property = "email", column = "email"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "avatar", column = "avatar")
    })
    Users queryByEmail(String email);
}